package com.rpc.center.register.client.netty;

import com.rpc.common.entity.response.DiscoverRS;
import com.rpc.common.entity.response.PublishRS;
import com.rpc.common.entity.response.ResponsePackage;
import com.rpc.common.entity.response.ResponseType;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.Getter;
import lombok.Setter;

import java.util.concurrent.CountDownLatch;

public class NettyRegistServiceHandler extends SimpleChannelInboundHandler<ResponsePackage> {
    private NettyRegisterServiceImpl registerService;
    @Getter
    private boolean isPublishSuccess;
    @Setter
    private CountDownLatch latch;
    public NettyRegistServiceHandler(NettyRegisterServiceImpl registerService) {
        this.registerService = registerService;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ResponsePackage responsePackage) throws Exception {
        if(responsePackage.getResponseType()==ResponseType.PUBLISH_SUCCESS){
            System.out.println("收到注册中心数据："+responsePackage.getMsg());
            PublishRS publishRS=(PublishRS) responsePackage;
            if(publishRS.isSuccess()){
                isPublishSuccess =true;
            }else{
                isPublishSuccess =false;
            }
            if(latch!=null){
                latch.countDown();
                latch=null;
            }
        }
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //super.channelActive(ctx);
        //registerService.setChannel(ctx.channel());
        System.out.println("连接注册中心成功");
    }


}
